import axios from "axios";
import { put, takeEvery } from "redux-saga/effects";
import {
  getSongDetailFailed,
  getSongDetailSuccess,
  GET_SONG_DETAIL,
  IGetSongDetailAction,
} from "../actions/songdetail.action";

function* handleGetSongDetail(action: IGetSongDetailAction) {
  try {
    const { data } = yield axios.get(
      "http://localhost:3001/song/detail?ids=" + action.payload
    );
    yield put(getSongDetailSuccess(data));
  } catch (error) {
    yield put(getSongDetailFailed(JSON.stringify(error)));
  }
}

export default function* songDetailSaga() {
  yield takeEvery(GET_SONG_DETAIL, handleGetSongDetail);
}
